package com.aysafety.iotdataplatformweb.controller.integrationWarning;

import com.aysafety.core.common.CheckUtils;
import com.aysafety.core.common.DataConversionUtil;
import com.aysafety.core.common.DateUtils;
import com.aysafety.iotdataplatformweb.controller.base.BaseController;
import com.aysafety.iotdataplatformweb.services.integrationWarning.MultiParameterIntegratedWarningService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/mineMonitor/realMonitor/integrationWarning")
public class MultiParameterIntegratedWarningController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(MultiParameterIntegratedWarningController.class);

    @Autowired
    private MultiParameterIntegratedWarningService multiParameterIntegratedWarningService;

    @RequestMapping("/comprehensiveWarning")
    public Map<String, Object> comprehensiveWarning(@RequestParam Map<String, Object> params,
                                                    HttpServletRequest request,
                                                    HttpServletResponse response) {
        try {
            Map<String, Object> data = new HashMap<String, Object>();

            // 矿区id
            String mineId = DataConversionUtil.getStrFromMap("mineId", params);

            // 设备id
            //String deviceId = DataConversionUtil.getStrFromMap("equipmentId", params);

            // 开始时间
            String startTime = DataConversionUtil.getStrFromMap("startTime", params);

            if (StringUtils.isBlank(mineId) || StringUtils.isBlank(startTime)) {
                return this.formatReturnData("缺少参数");
            }
            if (!CheckUtils.isValidDate(startTime, "yyyy-MM-dd HH:mm:ss")) {
                return this.formatReturnData("日期格式不正确");
            }
            // 结束时间
            String defaultEndTime = DateUtils.getCurrentTime("yyyy-MM-dd HH:mm:ss");
            String endTime = DataConversionUtil.getStrFromMapOrSetDefaultValue("endTime", defaultEndTime, params);

            Map<String, Object> results = multiParameterIntegratedWarningService.comprehensiveWarning(mineId, startTime, endTime);
            data.put("list", results);
            return this.formatReturnData(data);
        } catch (Exception e) {
            logger.error("multiParameterIntegratedWarning:", e);
            return this.formatReturnData(e.getMessage());
        } catch (Throwable throwable) {
            logger.error("multiParameterIntegratedWarning:", throwable);
            return this.formatReturnData(throwable.getMessage());
        }
    }

}
